package com.example.yrdev.ordering_system.homeOptions;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;

import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.Common.Setting;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import com.example.yrdev.ordering_system.Utils.MIScanner;

import android.os.Handler;

public class MainActivity extends CommonActivity {

    PaymentDialog paymentDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GlobalFunction.initLanguage(this);

        setBarTitle(getResources().getString(R.string.home));
        setContentView(R.layout.main_page_layout);

           initNFCParam();
          initView();

   /*     FixPriceTimeSchedule.getInstance().getCurrentFixPrice("1");
        try {
          boolean stat=  GlobalFunction.isTimeBetweenTwoTime("10:00:00","17:00:00");
        GlobalFunction.printTextByTag("stat:"+stat);
        }catch (Exception e)
        {

        }*/


    }

private  void testFunction(){
    //        PurchaseReport report=new PurchaseReport();
//        report.setCommodityID("001");
//        report.setCommodityName("555碱性（7号)电池");
//        report.setSaleCost("10.00");
//        report.setCost("10.00");
//        report.setVolume(1);
//
//        PurchaseReport report2=new PurchaseReport();
//        report2.setCommodityID("001");
//        report2.setCommodityName("a苹果");
//        report2.setSaleCost("10.00");
//        report2.setCost("10.00");
//        report2.setVolume(1);
//
//        ArrayList<PurchaseReport> goodArrayList=new ArrayList<>();
//        goodArrayList.add(report);
//        goodArrayList.add(report2);

    //  MIReaderUtils.convertToPurchaseReceipt("aaa","2131","123",goodArrayList);


}
   // mainFragment2 botFragment;
int paymentType;


    @Override
    public void initView() {
        super.initView();
    }

    public void homeOptions(View view){

        switch (view.getId()){
                  case R.id.menu:

                     gotoActivity(MenuActivity.class, 0);
                     activatePushingAnim();

                      break;
                  case R.id.commodity:

                   /*   if (Setting.getInstance().checkWorkingMode(2))

                      gotoActivity(CommodityActivity.class,0);
                      activatePushingAnim();*/
                      break;
                  case R.id.fixedPrice:
                      processCardState=true;
                      paymentType=0;
                      if (Setting.getInstance().checkWorkingMode(1))
                          startPaymentDialog(processCardState);
                      break;
                  case R.id.randomPrice:
                  /*    processCardState=false;
                        paymentType=1;

                      if (Setting.getInstance().checkWorkingMode(0))
                          startPaymentDialog(processCardState);
                          */
                      break;
                  default:
                 /*   processCardState=false;
                       paymentType=0;
                      int temp=1;

                      if (index==R.id.fixedPrice){
                          processCardState=true;

                      }
                      if (index==R.id.randomPrice){
                //          paymentType=1;
                  //        temp=0;

                      }



                      if (Setting.getInstance().checkWorkingMode(temp))
                         startPaymentDialog(processCardState);

*/






                      break;


              }



          }


private void startPaymentDialog(boolean stat){

    paymentDialog  =new PaymentDialog(MainActivity.this,paymentType,MainActivity.this);
    paymentDialog.show();
    if (stat){
        paymentDialog.startPurchaseMode();
    }
    if (paymentType==0)
        paymentDialog.hidePriceField();


    reader.setReduceAmount(paymentDialog.getCurrentPrice());
    reader.setOperate(1);//转成写卡
    reader.setAction(1);
    reader.setPaymentMode(paymentType);

    paymentDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            reader.reset();
            processCardState=true;

        }
    });

    paymentDialog.setCallBack(new PaymentDialog.callBack() {
        @Override
        public void getCurAmount(Integer amount) {

            processCardState=true;
            reader.setReduceAmount(amount);
        }

        @Override
        public void done() {

        }

        @Override
        public void standbyMode() {
            processCardState=false;

        }
    });
    reader.setCallback(new MIScanner.Callback() {
        @Override
        public void showFixPurchaseCost(Double cost) {

            //paymentDialog.showFixPricePurchaseCost(cost);
            paymentDialog.dismiss();


        }

        @Override
        public void getCardInfo(Double reduceAmount,Double remain, Double currentBalance) {

             paymentDialog.showOtherNominal(reduceAmount,remain,currentBalance);

        }

        @Override
        public void getMICardNo(String cardNo,String ticketID) {

        }
    });


    //进行扣钱
}
//for reading MI CARD




/*
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode==KeyEvent.KEYCODE_BACK){
        if (paymentDialog!=null){

            if (paymentDialog.isShowing()){
                paymentDialog.dismiss();
                paymentDialog=null;

            }
        }
       else {


        }
    }
        return super.onKeyDown(keyCode, event);

    }
*/
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    try {
        if (processCardState)
        reader.processIntent(mNfcAdapter, intent);
    }catch (Exception e){
        e.printStackTrace();
    }


    }

    @Override
    protected void onResume() {
        super.onResume();
        GlobalFunction.printTextByTag("process");
        try {
            if (processCardState)
                reader.processIntent(mNfcAdapter,getIntent());

        }catch (Exception e){

        }

    }


/*
    public void restartFragment(){

        final FragmentTransaction ft =getSupportFragmentManager().beginTransaction();
        ft.detach(botFragment);
        ft.attach(botFragment);
        ft.commit();
        setBarTitle(getResources().getString(R.string.home));

    }*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
      //  restartFragment();
    }
/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
      //  getMenuInflater().inflate(R.menu.m｀enu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }

        return super.onOptionsItemSelected(item);
    }

    public static class  mainFragment1 extends Fragment{
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView=inflater.inflate(R.layout.main_page_layout_fragment1,container,false);

            return rootView;

        }
    }
    */
    boolean doubleBackToExitPressedOnce = false;

    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            closeApp();


            return;
        }

        this.doubleBackToExitPressedOnce = true;
        GlobalFunction.ToastAlert(MainActivity.this,"再按一次退出程序");


        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
            }
        }, 2000);
    }
    private void closeApp(){
        Intent homeIntent = new Intent(Intent.ACTION_MAIN);
        homeIntent.addCategory( Intent.CATEGORY_HOME );
        homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(homeIntent);

    }
/*
    public static class  mainFragment2 extends Fragment{

      public   mainFragment2(){

        }
        public interface CallBack{
            void menuSelected(Integer index);
        }
        CallBack callback;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

           // GlobalFunction.initLanguage(getActivity(),"en");

            View rootView=inflater.inflate(R.layout.main_page_layout_fragment2,container,false);
           LinearLayout menu=(LinearLayout)rootView.findViewById(R.id.menu);
            LinearLayout commodity=(LinearLayout)rootView.findViewById(R.id.commodity);
             LinearLayout fixedPrice=(LinearLayout)rootView.findViewById(R.id.fixedPrice);
            LinearLayout randomPrice=(LinearLayout)rootView.findViewById(R.id.randomPrice);

            menu.setOnClickListener(optionListener());
            commodity.setOnClickListener(optionListener());
            fixedPrice.setOnClickListener(optionListener());
            randomPrice.setOnClickListener(optionListener());

            return rootView;

        }

        public void setCallback(CallBack callback) {
            this.callback = callback;
        }

        View.OnClickListener optionListener(){
            View.OnClickListener listener=new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (callback!=null){
                        callback.menuSelected(view.getId());
                    }
                }
            };
            return listener;

        }
    }*/
}
